layui.use(['table', 'admin', 'ax', 'func'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;

    /**
     * 优惠卷管理
     */
    var FlowCoupon = {
        tableId: "flowCouponTable"
    };

    /**
     * 初始化表格的列
     */
    FlowCoupon.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'couponId', hide: true, title: 'ID'},
            {field: 'couponCode', sort: true, title: '优惠卷码'},
            {field: 'moneys', sort: true, title: '金额'},
            {field: 'isUsed', sort: true, title: '是否使用',templet:function (row) {
                    if(row.isUsed == 0){
                        return "未使用";
                    }else{
                        return "已使用";
                    }
                }},
            {field: 'isGet', sort: true, title: '是否获取',templet:function (row) {
                    if(row.isGet == -1){
                        return "未获取";
                    }else{
                        return "已获取";
                    }
            }},
            {field: 'onprice', sort: true, title: '满金额使用'},
            {field: 'serviceType', sort: true, title: '使用服务',templet:function (row) {
                    if(row.serviceType == -1){
                        return "不限制";
                    }else{
                        return row.serviceName;
                    }
            }},
            {field: 'createTime', sort: true, title: '创建时间'},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    FlowCoupon.search = function () {
        var queryData = {};


        table.reload(FlowCoupon.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    FlowCoupon.onselectItem = function (data) {
        var iframs = top.document.getElementsByTagName('iframe');
        for (var i = 0;i<iframs.length;i++){
            if(iframs[i].contentWindow.PFun){
                iframs[i].contentWindow.PFun.chooseCoupon(data.couponId,data.couponTitle,data.moneys);
            }
        }
        admin.closeThisDialog();
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + FlowCoupon.tableId,
        url: Feng.ctxPath + '/flowOrder/flowCouponList?zfbMemberId='+Feng.getUrlParam("zfbMemberId") + "&bdMemberId="+Feng.getUrlParam("bdMemberId")+"&serviceType="+Feng.getUrlParam("serviceType"),
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: FlowCoupon.initColumn()
    });

    // 工具条点击事件
    table.on('tool(' + FlowCoupon.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'select') {
            FlowCoupon.onselectItem(data);
        }
    });
});
